import { ExecutionEngine } from './core/execution-mode';
import { ExecutionMode } from './core/execution-mode';
import type { ExecutionOptions } from './core/execution-mode';

type TaskFunction<T = any> = () => T | Promise<T>;

/**
 * @deprecated 请使用MultiModeEngine替代
 */
export class CoreSystem extends MultiModeEngine {
  public start(): Promise<void> {
    return Promise.resolve();
  }
  
  public grpcServer = {
    shutdown: () => Promise.resolve()
  };
  
  public fileAdapter = {
    processFile: (path: string) => Promise.resolve({success: true})
  };
  
  public aiRouter = {
    hasCapability: () => false,
    process: () => Promise.resolve({status: 'ok'})
  };
}

export class MultiModeEngine {
  private engine: ExecutionEngine;
  private defaultOptions: Partial<ExecutionOptions>;

  constructor(defaultOptions: Partial<ExecutionOptions> = {}) {
    this.engine = new ExecutionEngine();
    this.defaultOptions = {
      mode: ExecutionMode.ACTIVE,
      ...defaultOptions
    };
  }

  /**
   * 快速执行方法
   */
  static async run<T>(
    task: TaskFunction<T>,
    options?: Partial<ExecutionOptions>
  ): Promise<T> {
    const instance = new MultiModeEngine(options);
    return instance.execute(task);
  }

  /**
   * 设置默认执行模式
   */
  setMode(mode: ExecutionMode): this {
    this.defaultOptions.mode = mode;
    return this;
  }

  /**
   * 获取当前执行模式
   */
  getMode(): ExecutionMode {
    return this.defaultOptions.mode || ExecutionMode.ACTIVE;
  }

  /**
   * 设置确认提示信息
   */
  setConfirmMessage(message: string): this {
    this.defaultOptions.confirmMessage = message;
    return this;
  }

  /**
   * 设置触发条件
   */
  setTriggerCondition(condition: () => boolean): this {
    this.defaultOptions.triggerCondition = condition;
    return this;
  }

  /**
   * 执行任务
   */
  async execute<T>(
    task: TaskFunction<T>,
    overrideOptions: Partial<ExecutionOptions> = {}
  ): Promise<T> {
    const options: ExecutionOptions = {
      ...this.defaultOptions,
      ...overrideOptions
    } as ExecutionOptions;

    try {
      const result = await this.engine.execute(async () => {
        const res = await task();
        return res as unknown as void;
      }, options);
      return result as unknown as T;
    } catch (error) {
      throw this.normalizeError(error);
    }
  }

  private normalizeError(error: unknown): Error {
    if (error instanceof Error) {
      return error;
    }
    return new Error(`Execution failed: ${String(error)}`);
  }

  // 快捷方法
  active = this.createModeProxy(ExecutionMode.ACTIVE);
  passive = this.createModeProxy(ExecutionMode.PASSIVE);
  semiPassive = this.createModeProxy(ExecutionMode.SEMI_PASSIVE);
  automatic = this.createModeProxy(ExecutionMode.AUTOMATIC);
  triggered = this.createModeProxy(ExecutionMode.TRIGGERED);
  intelligent = this.createModeProxy(ExecutionMode.INTELLIGENT);

  private createModeProxy(mode: ExecutionMode) {
    return {
      execute: <T>(task: TaskFunction<T>, options: Omit<Partial<ExecutionOptions>, 'mode'> = {}) => 
        this.execute(task, { ...options, mode })
    };
  }
}

// 使用示例：
/*
// 方式1：快速调用
MultiModeEngine.run(() => console.log('Quick execution'));

// 方式2：实例化使用
const engine = new MultiModeEngine()
  .setMode(ExecutionMode.SEMI_PASSIVE)
  .setConfirmMessage('确认执行？');

engine.execute(() => console.log('Running with confirmation'));

// 方式3：快捷方法
engine.automatic.execute(() => console.log('Auto retry execution'));
*/

// 平台相关导出
export { Platform, getCurrentPlatform } from './core/platform';
export { ExecutionMode } from './core/execution-mode';
export type { ExecutionOptions };